package org.jeecg.modules.flow.callback.move;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptPage;
import org.jeecg.modules.flow.callback.move.util.CommonMoveUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 移库提交
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-10-12
 */
@Component
public class MoveReceiptCommitBean<T> extends BaseCustomBeanRun<T> {

    @Autowired
    private IWmsRepertoryService wmsRepertoryService;

    @Autowired
    private IWmsMoveReceiptService wmsMoveReceiptService;

    @Autowired
    private IWmsMoveReceiptMaterialService wmsMoveReceiptMaterialService;

    @Resource
    private CommonMoveUtil commonMoveUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsMoveReceiptPage.class);
        WmsMoveReceiptPage wmsMoveReceiptPage = (WmsMoveReceiptPage) baseFlowDto.getData();
        String receiptId = wmsMoveReceiptPage.getId();
        WmsMoveReceipt wmsMoveReceipt = new WmsMoveReceipt();
        BeanUtils.copyProperties(wmsMoveReceiptPage, wmsMoveReceipt);
        List<WmsMoveReceiptMaterial> wmsMoveReceiptMaterialList;
        if (StrUtil.isEmpty(receiptId)) {
            receiptId = wmsMoveReceiptService.saveMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
            // 校验库存
            wmsRepertoryService.moveSubmit(receiptId);
            wmsMoveReceiptMaterialList = wmsMoveReceiptPage.getWmsMoveReceiptMaterialList();
        } else {
            if (CollectionUtil.isNotEmpty(wmsMoveReceiptPage.getWmsMoveReceiptMaterialList())) {
                wmsMoveReceiptService.updateMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
            }
            // 校验库存
            wmsRepertoryService.moveSubmit(receiptId);
            wmsMoveReceipt = wmsMoveReceiptService.getById(receiptId);
            if (Objects.isNull(wmsMoveReceipt)) {
                throw new JeecgBootException("移库单id错误");
            }
            wmsMoveReceiptMaterialList = wmsMoveReceiptMaterialService.list(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                    .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, receiptId));
        }
        //修改实时库存
        commonMoveUtil.moveRepertory(receiptId, wmsMoveReceipt.getTransferFromWarehouseId(), wmsMoveReceiptMaterialList);
        wmsMoveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                .set(WmsMoveReceipt::getDocumentStatus, 200)
                .eq(WmsMoveReceipt::getId, receiptId));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        baseFlowDto.setInfoId(receiptId);
    }
}
